package com.rchat.pocmini.activity.invitefriendtogroup;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.airtalkee.sdk.AirtalkeeAccount;
import com.rchat.pocmini.R;
import com.rchat.pocmini.bean.UserNoInGroupBean;
import com.rchat.pocmini.bean2eventbus.SelectedFriendInviteToGroupBean;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;

/**
 * Created by ljd on 2017/9/1.
 */

public class InviteFriendNoInGroupAdapter extends BaseAdapter{

    private LayoutInflater inflater;
    private ArrayList<UserNoInGroupBean.DataBean.UsersBean.UserBean> data;

    public InviteFriendNoInGroupAdapter(Context context,ArrayList<UserNoInGroupBean.DataBean.UsersBean.UserBean> data){
        this.data = data;
        inflater = LayoutInflater.from(context);
    }

    public void setData(ArrayList<UserNoInGroupBean.DataBean.UsersBean.UserBean> data){
        this.data = data;
    }

    @Override
    public int getCount() {
        return data==null?0:data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if (null == convertView) {
            convertView = inflater.inflate(R.layout.search_listview_item, null);
            holder = new ViewHolder();
            holder.tvDisplayName = (TextView) convertView.findViewById(R.id.textView1);
            holder.tvUserName = (TextView) convertView.findViewById(R.id.textView2);
            holder.cb = (CheckBox) convertView.findViewById(R.id.cb_selectmenbes);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        if(data.get(position).getUserName().equals(AirtalkeeAccount.getInstance().getUserId())){
            holder.cb.setVisibility(View.GONE);
        }else {
            holder.cb.setVisibility(View.VISIBLE);
        }

        if(data.get(position).isSelected()){
            holder.cb.setChecked(true);
        }else {
            holder.cb.setChecked(false);
        }

        holder.tvDisplayName.setText(data.get(position).getDisplay());
        holder.tvUserName.setText(data.get(position).getUserName());

        holder.cb
                .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(
                            CompoundButton buttonView, boolean isChecked) {
                        SelectedFriendInviteToGroupBean bean = new SelectedFriendInviteToGroupBean();
                        bean.setTarget(this.getClass(),InviteFriendToGroupPresenterImpl.class,"onEventBusInviteFriend");
                        bean.setCurrentEvent(SelectedFriendInviteToGroupBean.SELECTED_FRIEND_INVITE_TO_GROUP);
                        bean.setCheck(isChecked);
                        bean.setUserName(data.get(position).getUserName());
                        EventBus.getDefault().post(bean);


//                        checkMap.put(position, isChecked);
//                        isHasCheck(position);

                        data.get(position).setSelected(isChecked);
                    }
                });
        return convertView;

    }

    static class ViewHolder{
        private TextView tvDisplayName;
        private TextView tvUserName;
        private CheckBox cb;
    }
}
